function checkLen(obj) {
    var maxChars = 300;
    if (obj.value.length > maxChars)obj.value = obj.value.substring(0,maxChars);
}

function purchaseadd(url,day){
    var eName = $.trim($("input[name='eName']").val());
    var eFile = $.trim($("input[name='eFile']").val());
    var eOldName = $.trim($("input[name='eOldName']").val());
    if(eName == ''){
    //    alert('清单名称必须填写');
    //    return false;
    }
    if(eName.length>18){
    //    alert('清单名称不能超过18个汉字');
    //    return false;
    }
    if(eFile == ''){
        alert('必须上传清单文件');
        return false;
    }

    var ePeople = $.trim($("input[name='ePeople']").val());
    var eRemark = $.trim($("input[name='eRemark']").val());
    var ePhone = $.trim($("input[name='ePhone']").val());
    var eMobile = $.trim($("input[name='eMobile']").val());
    var eQQ = $.trim($("input[name='eQQ']").val());
    var eType = $('input[name="eType"]:checked').val();

    closeParent();
    // showParent(700,'popup_content_load','清单名称：'+eName+'　上传时间：'+day+'　正在匹配商品');
    $.ajax({
        type:'post',
        url:url,
        data:'eName='+eName+'&eFile='+eFile+'&eOldName='+eOldName+'&ePeople='+ePeople+'&eRemark='+eRemark+'&ePhone='+ePhone+'&eMobile='+eMobile+'&eQQ='+eQQ+'&eType='+eType,
        dataType:'json',
        success:function(response){
            closeParent();
            if(response.ret == 200){
                top.window.location.reload();
            }else if(response.ret == 1000){
                top.window.location.href = 'mMPur-indexlist.html&pkey=' + response.jid;
            }else{
                //showParent(500,'popup_content_alert',response.msg);
                confirmError(response.msg,'child');
            }
        }
    });
}

function purchasealter(url){ 
    var eName = $.trim($("input[name='eName']").val());
    var eKey = $.trim($("input[name='eKey']").val());
    if(eName == ''){
        alert('清单名称必须填写');
        return false;
    }
    if(eKey == '' || isNaN(eKey)){
        alert('当前清单不存在，请刷新页面重试');
        return false;
    }
    var eRemark = $.trim($("input[name='eRemark']").val());
    var ePeople = $.trim($("input[name='ePeople']").val());
    var ePhone = $.trim($("input[name='ePhone']").val());
    var eMobile = $.trim($("input[name='eMobile']").val());
    var eQQ = $.trim($("input[name='eQQ']").val());

    var data = 'eKey='+eKey+'&eName='+eName+'&ePeople='+ePeople+'&eRemark='+eRemark+'&ePhone='+ePhone+'&eMobile='+eMobile+'&eQQ='+eQQ;
    doWork(url,data,'child');
}

function faqadd(url){
    var eContent = $.trim($("textarea[name='eContent']").val());
    var ePKey = $.trim($("input[name='ePKey']").val());
    if(eContent == ''){
        alert('需要帮助的内容必须填写');
        return false;
    }
    if(ePKey == '' || isNaN(ePKey)){
        alert('请求帮助的清单不存在，请刷新页面重试');
        return false;
    }
    var eFile = $.trim($("input[name='eFile']").val());
    var ePeople = $.trim($("input[name='ePeople']").val());
    var ePhone = $.trim($("input[name='ePhone']").val());
    var eQQ = $.trim($("input[name='eQQ']").val());

    var data = 'ePKey='+ePKey+'&eContent='+eContent+'&eFile='+eFile+'&ePeople='+ePeople+'&ePhone='+ePhone+'&eQQ='+eQQ;
    doWork(url,data,'child');
}

function goodsalter(url){
    var eName = $.trim($("input[name='eName']").val());
    var eNumber = $.trim($("input[name='eNumber']").val());

    var purNum = $("#purNum",window.parent.document).attr('data-num');
    if(parseInt(purNum) >= 200){
        alert('清单已超200上限，不能再添加了');
        return false;        
    }
    if(eName == ''){
        alert('商品名称必须填写');
        return false;
    }
    if(eNumber == '' || isNaN(eNumber)){
        alert('数量必须填写且只能为数字');
        return false;
    }
    if(eNumber <= 0){
        alert('数量必须大于0');
        return false;
    }

    var eBrand = $.trim($("input[name='eBrand']").val());
    var eColor = $.trim($("input[name='eColor']").val());
    var eModel = $.trim($("input[name='eModel']").val());
    var ePack = $.trim($("input[name='ePack']").val());
    var eOldNumber = $.trim($("input[name='eOldNumber']").val());
    var eNumber = $.trim($("input[name='eNumber']").val());
    var eRemark = $.trim($("input[name='eRemark']").val());
    var eKey = $.trim($("input[name='eKey']").val());
    var pKey = $.trim($("input[name='pKey']").val());
    var pType = $.trim($("input[name='pType']").val());

    var data = 'eKey='+eKey+'&eName='+eName+'&eBrand='+eBrand+'&eColor='+eColor+'&eModel='+eModel+'&ePack='+ePack+'&eOldNumber='+eOldNumber+'&eNumber='+eNumber+'&eRemark='+eRemark+'&pKey='+pKey+'&pType='+pType+'&purNum='+purNum;
    doWork(url,data,'child');

}

function checkall(domId,chkId){
    if($('#'+domId).is(":checked")){
        $("input[name='"+chkId+"']").each(function(){
            if($(this).is(":disabled")){
                $(this).attr("checked",false);
            }else{
                $(this).attr("checked",true);
            }
        });
    }else{
        $("input[name='"+chkId+"']").attr("checked",false);
    }
}

function warehouseSearch(){
    var view = $.trim($("input[name='view']").val());
    var sname = $.trim($("input[name='sname']").val());
    var smodel = $.trim($("input[name='smodel']").val());
    var sbrand = $.trim($("input[name='sbrand']").val());
    var pkey = $.trim($("input[name='pkey']").val());
    var url = '/index.php?app=member/BPurchase&act=warehouse&pkey='+encodeURIComponent(pkey)+'&view='+encodeURIComponent(view)+'&sname='+encodeURIComponent(sname)+'&smodel='+encodeURIComponent(smodel)+'&sbrand='+encodeURIComponent(sbrand);
    $('#searchForm').attr('action',url);
    return true;
}

function batch(domId,url,data,from){
    var checkedId = '';
    $("input[name='"+domId+"']").each(function(){
        if($(this).is(":checked")){
            checkedId += $(this).val()+',';
        }
    });
    if(checkedId == ''){
        alert('请至少选择一条数据');
        return false;
    }
    var chklength = checkedId.length;
    chklength = chklength-1;
    checkedId = checkedId.substr(0,chklength);
    data += checkedId;
    var purNum = $("#purNum",window.parent.document).attr('data-num');
    data += '&purNum='+purNum;
    doWork(url,data,from,data);
}
function confimWork(domId,url,msg,data){
    showLayer(500,domId,msg,function(){
    	doWork(url,data,'self')
    });
}
function confimWorkAll(chkId,domId,url,msg,data){
    var checkedId = '';
    $("input[name='"+chkId+"']").each(function(){
        if($(this).is(":checked")){
            checkedId += $(this).val()+',';
        }
    });
    if(checkedId == ''){
        alert('请至少选择一条数据');
        return false;
    }
    var chklength = checkedId.length;
    chklength = chklength-1;
    checkedId = checkedId.substr(0,chklength);
    data += checkedId;
    confimWork(domId,url,msg,data)
}
function confirmOrder(){
    showLayer(500,'popup_content_confirm','清单中存在尚未确认的商品，请先确认！',function(){
        var url = window.location.href;
        var regExp = /&ptype=check/
        url = url.replace(regExp,'');
        url += '&ptype=check';
        top.window.location.href = url;
        return false;
    });

}
function confirmError(msg,from){
    switch(from){
        case 'self':
            showLayer(500,'popup_content_confirm',msg,function(){hidePop();return false;});
            break;
        case 'child':
            window.parent.showLayer(500,'popup_content_confirm',msg,function(){window.parent.hidePop();return false;});
            break;
    }
}
function doWork(url,data,from){
    $.ajax({
        type:'post',
        url:url,
        data:data,
        dataType:'json',
        success:function(response){
            switch(from){
                case 'self':
                    hidePop();
                    switch(response.ret){
                        case 200:
                        case 201:
                            showLayer(500,'popup_content_confirm',response.msg,function(){top.window.location.reload();});
                            break;
                        case 444:
                            data = data.replace('work=f','work=c');
                            showLayer(700,'popup_content_alert',response.msg,function(){doWork(url,data,from)});
                            break;
                        default:
                            confirmError(response.msg,from);
                            break;
                    }
                    break;
                case 'child':
                    closeParent();
                    if(response.ret == 200){
                        window.parent.showLayer(500,'popup_content_confirm',response.msg,function(){top.window.location.reload();});
                    }else{
                        confirmError(response.msg,from);
                    }
                    break;
            }
        }
    });
}

function uploadlist(type,key){
    switch(type){
        case 'up':
            $('#uplist').html('<IFRAME style="border: 0;" height="550px" width="100%" ID="Frame1" src="/index.php?app=member/BPurchase&act=uplist" allowTransparency="true"></IFRAME>');
            break;
        case 'alter':
            $('#uplist').html('<IFRAME style="border: 0;" height="450px" width="100%" ID="Frame1" src="/index.php?app=member/BPurchase&act=alterlist&pkey='+key+'" allowTransparency="true"></IFRAME>');
            break;
    }
    showLayer(450,'popup_content_up');
}

function uploadgoods(type,pkey,gkey,gname){
    switch(type){
        case 'faq':
            $('#upgoods').html('<IFRAME style="border: 0;" height="500px" width="100%" ID="Frame1" src="/index.php?app=member/BPurchase&act=faq&pkey='+pkey+'" allowTransparency="true"></IFRAME>');
            showLayer(450,'popup_content_up');
            break;
        case 'up':
            $('#upgoods').html('<IFRAME style="border: 0;" height="450px" width="100%" ID="Frame1" src="/index.php?app=member/BPurchase&act=altergoods&pkey='+pkey+'&ptype='+gname+'&key='+gkey+'" allowTransparency="true"></IFRAME>');
            showLayer(450,'popup_content_up');
            break;
        case 'list':
            $('#upgoods').html('<IFRAME style="border: 0;" height="450px" width="100%" ID="Frame1" src="/index.php?app=member/BPurchase&act=alterlist&pkey='+key+'" allowTransparency="true"></IFRAME>');
            break;
        case 'choose':
            $('#upgoods2').html('<IFRAME style="border: 0;" height="600px" width="100%" ID="Frame1" src="/index.php?app=mall/MGoodsChoose&act=choose&from=u&pk='+pkey+'&gk='+gkey+'&pn='+encodeURIComponent(gname)+'" allowTransparency="true"></IFRAME>');
            showLayer(700,'popup_content_up2');
            break;
        case 'warehouse':
            $('#upgoods3').html('<IFRAME style="border: 0;" height="500px" width="100%" ID="Frame1" src="/index.php?app=member/BPurchase&act=warehouse&view=c&pkey='+pkey+'" allowTransparency="true"></IFRAME>');
            showLayer(1000,'popup_content_up3');
            break;
    }

}

function closeParent(){
    $(window.parent.document).find(".layui-layer-page").css("display","none");
    $(window.parent.document).find(".layui-layer-shade").css("display","none");
}

function showParent(width,domId,msg){
    window.parent.showLayer(width,domId,msg);
}

function pageChange(url){
    var pagesize = $('#perpage').val();
    $.ajax({
        type:'post',
        url:url,
        data:'pagesize='+pagesize,
        dataType:'json',
        success:function(response){
            var url = window.location.href;
            var regExp = /&page=\d/
            url = url.replace(regExp,'');
            top.window.location.href = url;
        }
    });
}
function bao(dom){
    $('#'+dom).show();
}
function baooff(dom){
    $('#'+dom).hide();
}

$(".J-img").mouseenter(function  () {
	var src=$(this).attr("src");
	$(this).siblings(".J-img-big").attr("src",src);
	$(this).siblings(".J-img-big").show();
})
$(".J-img").mouseleave(function  () {
	$(this).siblings(".J-img-big").hide();
})
